/*
* Copyright (C) 2010-2012 Magnus Skjegstad
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
* 
* The latest version of this library can be downloaded from http://mist-pubsub.googlecode.com
* 
*/
package com.skjegstad.mist2.demos;

import com.skjegstad.mist2.IMistSubscriptionListener;
import com.skjegstad.mist2.MistCoreManager;
import com.skjegstad.mist2.datatypes.DataElement;
import com.skjegstad.mist2.exceptions.MistException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 *
 * @author Magnus Skjegstad
 */
public class TestUnicastFileTransfer {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws InterruptedException, MistException, UnknownHostException {


        MistCoreManager master = new MistCoreManager(1);
        master.setDebug(true);
        master.enableUDPUnicastServer(1234);
        master.subscribe("no.ffi", new IMistSubscriptionListener() {
            public void deliverMessage(DataElement message) {
                System.out.println("RECEIVED DATA ELEMENT in node 1");
            }
        });
        master.start();

        int nodes = 5;
        MistCoreManager[] slaves = new MistCoreManager[nodes];


        for (int i = 0; i < nodes; i++) {
            final int nodeId = i+2;
            slaves[i] = new MistCoreManager(nodeId);
            slaves[i].setDebug(true);
            slaves[i].enableUDPUnicastServer(1235+i);
            slaves[i].subscribe("no.ffi", new IMistSubscriptionListener() {
                public void deliverMessage(DataElement message) {
                    System.out.println("RECEIVED DATA ELEMENT in node " + nodeId);
                }
            });
            slaves[i].start();
            slaves[i].addUDPUnicastNeighbor(InetAddress.getByName("127.0.0.1"), 1234);
        }


        master.publish("no.ffi", new byte[1000000]);


        System.out.println("Sleeping.");
        for (int i = 0; i < 60; i++) {
            Thread.sleep(1000);
            System.out.print(".");
        }

        System.out.println("Stopping master...");
        master.stop();

        System.out.println("Stopping slaves...");
        for (int i = 0; i < nodes; i++) {
            System.out.println("Slave " + slaves[i].getMyNodeId().getNodeIdentifier());
            if (slaves[i].getDataElements().size() != master.getDataElements().size())
                System.err.println("Slave " + slaves[i].getMyNodeId().getNodeIdentifier() + " not synchronized!");
            slaves[i].stop();
        }
        
    }

}
